package com.atguigu.guli.service.edu.controller.admin;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.form.CourseInfoForm;
import com.atguigu.guli.service.edu.entity.query.CourseQuery;
import com.atguigu.guli.service.edu.entity.vo.CourseVo;
import com.atguigu.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "课程管理")
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/admin/edu/course")
public class CourseController {

    @Autowired
    CourseService courseService;

    @ApiOperation("新增课程")
    @PostMapping("save-course-info")
    public R saveCourseInfo(@ApiParam(value = "课程基本信息",required = true) @RequestBody CourseInfoForm courseInfoForm){
        String courseId = courseService.saveCourseInfo(courseInfoForm);
        return R.ok().data("courseId",courseId).message("保存成功");
    }

    @ApiOperation("根据ID查询课程")
    @GetMapping("course-info/{id}")
    public R getById(@ApiParam(value = "id",required = true) @PathVariable String id){
        CourseInfoForm courseInfoForm = courseService.getCourseInfoById(id);
        if (courseInfoForm != null){
            return R.ok().data("item",courseInfoForm);
        }else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("更新课程")
    @PutMapping("update-course-info")
    public R updateCourseInfoById(@ApiParam(value = "课程基本信息",required = true) @RequestBody  CourseInfoForm courseInfoForm ){
        courseService.updateCourseInfoById(courseInfoForm);
        return R.ok().message("修改成功");
    }

    @ApiOperation("获取分页课程列表")
    @GetMapping("list/{page}/{limit}")
    public R getPageList(@ApiParam(value = "当前页码",required = true) @PathVariable Long page,
                         @ApiParam(value = "每页最大查询数",required = true) @PathVariable Long limit,
                         @ApiParam(value = "查询对象") CourseQuery courseQuery){
        IPage<CourseVo> courseVoIPage = courseService.selectPage(page, limit, courseQuery);
        return R.ok().data("pageList",courseVoIPage);
//        List<CourseVo> records = courseVoIPage.getRecords();
//        long total = courseVoIPage.getTotal();
//        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation("删除课程")
    @DeleteMapping("remove/{id}")
    public R removeById(@ApiParam(value = "根据id删除课程",required = true)@PathVariable String id){
        courseService.removeCoverById(id);
        boolean result = courseService.removeById(id);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("数据不存在");
        }
    }
}
